package xin.marcher.module.lifecycle.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xin.marcher.module.common.core.PageResult;
import xin.marcher.module.common.exception.BaseBizException;
import xin.marcher.module.common.exception.ProductErrorCodeEnum;
import xin.marcher.module.lifecycle.domain.dto.CategoryExpriWhiteDTO;
import xin.marcher.module.lifecycle.domain.request.CategoryExpriWhiteRequest;
import xin.marcher.module.lifecycle.domain.response.CategoryExpriWhiteResponse;
import xin.marcher.module.lifecycle.repository.CategoryExpriWhiteRepository;
import xin.marcher.module.lifecycle.service.CategoryExpriWhiteService;

import java.util.Objects;

/**
 *
 */
@Service
public class CategoryExpriWhiteServiceImpl implements CategoryExpriWhiteService {

    @Autowired
    private CategoryExpriWhiteRepository categoryExpriWhiteRepository;

    /**
     * 保存白名单
     *
     * @param request
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public CategoryExpriWhiteResponse saveExpriWhite(CategoryExpriWhiteRequest request) {
        // 入参检查
        checkParams(request);
        // 保存
        return categoryExpriWhiteRepository.saveExpriWhite(request);
    }

    /**
     * 修改白名单
     *
     * @param request
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public CategoryExpriWhiteResponse updateExpriWhite(CategoryExpriWhiteRequest request) {
        // 入参检查
        // 修改
        checkParams(request);
        return categoryExpriWhiteRepository.updateExpriWhite(request);
    }

    /**
     * 获取品类考核白名单，如果该品类没有设置白名单，那么就返回空的对象
     *
     * @param categoryId
     * @return
     */
    @Override
    public CategoryExpriWhiteDTO getExpriWhite(Integer categoryId) {
        // 入参检查
        if (Objects.isNull(categoryId)) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_ERROR.getErrorCode(), ProductErrorCodeEnum.PARAM_ERROR.getErrorMsg());
        }
        // 查询考核白名单
        return categoryExpriWhiteRepository.getExpriWhite(categoryId);
    }

    /**
     * 批量获取考核白名单
     *
     * @param request
     * @return
     */
    @Override
    public PageResult<CategoryExpriWhiteDTO> pageExpriWhite(CategoryExpriWhiteRequest request) {
        return categoryExpriWhiteRepository.pageExpriWhite(request);
    }


    private void checkParams(CategoryExpriWhiteRequest request) {
        if (Objects.isNull(request) || Objects.isNull(request.getCategoryId())) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_ERROR.getErrorCode(), ProductErrorCodeEnum.PARAM_ERROR.getErrorMsg());
        }
    }
}
